package com.songshuai.webapi.permission.controller;

import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import com.songshuai.webapi.permission.dto.request.NavigationAddReqDto;
import com.songshuai.webapi.permission.dto.request.NavigationRelateFunctionReqDto;
import com.songshuai.webapi.permission.dto.request.NavigationUpdateReqDto;
import com.songshuai.webapi.permission.dto.response.*;
import com.songshuai.webapi.permission.service.NavigationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api("菜单管理")
@Slf4j
@RestController
@RequestMapping("/api/navigations/")
public class NavigationsController extends BaseController{
    @Autowired
    private NavigationsService navigationsService;


    @ApiOperation(value = "根据appId获取登录用户的菜单列表")
    @GetMapping("queryLoginNavigationList")
    public QueryResponse<List<NavigationListResDto>> queryLoginNavigationList(@RequestParam("appId")Integer appId){
        List<NavigationListResDto> navigationList = navigationsService.queryLoginNavigationList(appId);
        return QueryResponse.setResData(navigationList,ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "根据id获取菜单信息")
    @GetMapping("queryNavigationById")
    public QueryResponse<NavigationResDto> queryNavigationById(@RequestParam("id") Integer id){
        NavigationResDto resDto = navigationsService.queryNavigationById(id);
        return QueryResponse.setResData(resDto,ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("addNavigation")
    public QueryResponse<NavigationAddOrUpResDto> addNavigation(@Valid @RequestBody NavigationAddReqDto parmDto, BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean lock = getTokenLock("/api/navigations/addNavigation");
        try {
            if (lock) {
                NavigationAddOrUpResDto resDto = navigationsService.addNavigation(parmDto);
                return QueryResponse.setResData(resDto, ResultStatus.SUCCESS);
            }
        } finally {
            relesesTokenLock("/api/navigations/addNavigation");
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("updateNavigation")
    public QueryResponse updateNavigation(@Valid @RequestBody NavigationUpdateReqDto parmDto, BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean lock = getTokenLock("/api/navigations/updateNavigation");
        try {
            if(lock) {
                NavigationAddOrUpResDto resDto = navigationsService.updateNavigation(parmDto);
                return QueryResponse.setResData(resDto, ResultStatus.SUCCESS);
            }
        } finally {
            relesesTokenLock("/api/navigations/updateNavigation");
        }
        return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }

    @ApiOperation(value = "删除菜单")
    @PostMapping("deleteNavigation")
    public QueryResponse deleteNavigation(@RequestParam("id") Integer id){
        try {
            boolean isSucc = navigationsService.deleteNavigation(id);
            if(isSucc){
                return new QueryResponse();
            }
        } catch (Exception e) {
            log.error("deleteNavigation",e);
        }
        return new QueryResponse(ResultStatus.ERROR_DELETE_FAILED);
    }

    @ApiOperation(value = "查询功能列表并根据菜单id返填")
    @GetMapping("queryFunctionListByNav")
    public QueryResponse<List<FunctionListQueryResDto>> queryFunctionListByNav(@RequestParam("navigationId")Integer navigationId, @RequestParam("appId")Integer appId){
        List<FunctionListQueryResDto> navigationList = navigationsService.queryFunctionListByNav(navigationId,appId);
        return QueryResponse.setResData(navigationList,ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "关联功能")
    @PostMapping("relateFunction")
    public QueryResponse relateFunction(@Valid @RequestBody NavigationRelateFunctionReqDto parmDto, BindingResult result){
        if(result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        boolean lock = getTokenLock("/api/users/relateFunction");
        try {
            if(lock) {
                boolean isSucc = navigationsService.relateFunction(parmDto);
                if (isSucc) {
                    return new QueryResponse();
                }
            }
        } catch (Exception e) {
            log.error("relateFunction",e);
        } finally {
            relesesTokenLock("/api/users/relateFunction");
        }
        return new QueryResponse(ResultStatus.ERROR_RELATE_FAILED);
    }

}
